﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
//
using System.IO;
using System.Globalization;

// MobiLib, Copyright Mirosław M. Mączka.  All Rights Reserved. This code released under the terms of the Microsoft Public License (MS-PL, http://opensource.org/licenses/ms-pl.html.)

namespace MobiLibrary
{
    public sealed class Utils
    {
        //
        private static TextInfo ti = null;
        static Utils()
        {
            ti = new CultureInfo("en-US", false).TextInfo;
        }
        //==================================================================================================================================
        //
        public static string CleanFileName(string s)
        {
            char[] invs = Path.GetInvalidFileNameChars();
            //
            s = s.Replace('<', 'ź');
            s = s.Replace('>', '╗');
            //
            s = s.Replace('"', '\'');
            //
            foreach (char c in invs)
            {
                s = s.Replace(c, '_');
            }
            // bo ja wiem...
            s = s.Replace('_', ' ');
            s.Replace("  ", " ");
            s.Replace("  ", " ");
            s.Replace("  ", " ");
            //
            s = RemoveAccents(s);
            s = RemoveQuotes(s);
            //
            s = s.Trim();
            //
            return s;
        }
        //--------------------------------------
        public static string RemoveAccents(string s)
        {
            string so = "";
            //
            s = s.Normalize(NormalizationForm.FormD);

            for (int ich = 0; ich < s.Length; ich++)
            {
                UnicodeCategory uc = CharUnicodeInfo.GetUnicodeCategory(s[ich]);
                if (uc != UnicodeCategory.NonSpacingMark)
                {
                    so += s[ich];
                }
            }
            //
            return so;
        }
        //--------------------------------------
        public static string RemoveQuotes(string s)
        {
            if (s.StartsWith("\"") && s.EndsWith("\""))
            {
                return s.Substring(1, s.Length - 2);
            }
            //
            return s;
        }
        //
        //==================================================================================================================================
        //
        public static string ToSentence(string src)
        {
            bool first = true;
            string s = "";
            foreach (char c in ti.ToLower(src))
            {
                if (first && !Char.IsWhiteSpace(c))
                {
                    first = false;
                    s += Char.ToUpper(c);
                }
                else
                {
                    s += c;
                }
            }
            //
            return s;
        }
        public static string ToFullName(string src)
        {
            return ti.ToTitleCase(ti.ToLower(src));
        }
        /*
        public static string swapLastFirstName(string s)
        {
            if (-1 != s.IndexOf(',')) return s;
            //
            List<string> ss = s.Split(' ').ToList();

            if (ss.Count < 2) return s;
            //
            string ln = ss[ss.Count-1].Trim();
            ss.RemoveAt(ss.Count - 1);
            //
            for(int i=0; i < ss.Count; i++)
            {
                ss[i] = ss[i].Trim() + " ";
            }
            string fn = string.Concat(ss).TrimEnd();
            //
            return ln + ", " + fn;
        }
         */
        //
        public static string swapLastFirstName(string fl)// Imie Nazwisko ==> Nazwisko, Imie
        {
            if (-1 != fl.IndexOf(',')) return fl;
            //
            fl = fl.Trim();
            fl = fl.Replace("  ", " ");
            //
            //
            string[] authors = fl.Split(new[] { '&' }, StringSplitOptions.RemoveEmptyEntries);
            for (int kk = 0; kk < authors.Length; kk++)
            {
                authors[kk] = authors[kk].Trim();
                //
                int k = authors[kk].LastIndexOf(" ");
                if (k < 0) continue;
                //
                string imie = authors[kk].Substring(0, k).Trim();
                string nazwisko = authors[kk].Substring(k + 1).Trim();
                //
                authors[kk] = nazwisko + ", " + imie;
                //
            }
            return String.Join(" & ", authors);
        }
        public static void ClearEXTH(MobiLib ml)
        {
            if (ml.IsEXTH)
            {
                //Contributor, Subject, asin, isbn
                ml.exthDeleteType(1);
                ml.exthDeleteType(2);
                ml.exthDeleteType(3);
                ml.exthDeleteType(103);
                ml.exthDeleteType(104);
                ml.exthDeleteType(105);
                ml.exthDeleteType(107);
                ml.exthDeleteType(108);
                ml.exthDeleteType(113);
                ml.exthDeleteType(208);
                ml.exthDeleteType(300);
                ml.exthDeleteType(403);
                ml.exthDeleteType(503);
                //
                ml.exthDeleteDoubles();
            }
        }
        //==================================================================================================================================
        //
    }
}
